package com.xjm.i1.core.resolver;

import com.xjm.i1.constant.AreaEnum;
import com.xjm.i1.core.resolver.impl.*;
import com.xjm.i1.core.struct.node.load.I1AreaCollections;
import lombok.extern.slf4j.Slf4j;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * i1数据解析器的适配器，适配多种区域的解析
 * */
@Slf4j
public class MetaDataResolveAdapter {

    private static List<MetaDataResolver> metaDataResolverList = new LinkedList<>();

    static {
        metaDataResolverList.add(new UpLeftColorDiffDensityResolver());
        metaDataResolverList.add(new UpRightColorDiffDensityResolver());
        metaDataResolverList.add(new DownLeftColorDiffDensityResolver());
        metaDataResolverList.add(new DownRightColorDiffDensityResolver());
        metaDataResolverList.add(new MiddleColorDiffDensityResolver());
        metaDataResolverList.add(new ColorContrastGrayResolver());
    }

    /**
     * 解析为加载数据的区域数据
     * @param metaData 原始数据二维数组
     * @param areaEnum 解析区域
     * @return I1AreaCollections 加载数据的区域数据
     * */
    public I1AreaCollections resolve(Double[][] metaData, AreaEnum areaEnum) {
        Optional<MetaDataResolver> first = metaDataResolverList.stream().filter(dataResolver -> dataResolver.resolverMatch(areaEnum)).findFirst();
        MetaDataResolver metaDataResolver = first.get();

        try {
            return metaDataResolver.resolve(metaData);
        } catch (Exception e) {
            log.error(e.getMessage(), e.getStackTrace());
            System.exit(-1);
        }
        return null;
    }
}
